import Core from "./core";
function getPosition(viewer) {
  this._viewer = viewer
  this._core = new Core();
}

/**
 * 获取当前摄像机的三维笛卡尔坐标
 */
getPosition.prototype.getPosition = function () {
  return this._viewer.camera.position;
}

/**
 * 获取当前摄像机的经纬度
 */
getPosition.prototype.getDegrees = function () {
  var cartographic = this._viewer.camera.positionCartographic;
  var Degrees = {
    lon: Cesium.Math.toDegrees(cartographic.longitude),
    lat: Cesium.Math.toDegrees(cartographic.latitude),
    height: cartographic.height
  };

  return Degrees;
};

/**
 * 获取鼠标当前三维笛卡尔坐标
 * @param {object} [movement] 鼠标屏幕位置
 * @param {Array/Object} [objectsToExclude] 排除的实体对象
 * @returns {Cartesian3}
 **/
getPosition.prototype.getMousePosition = function (movement, objectsToExclude) {
  let mousePosition = movement.endPosition || movement.position || movement;
  this.defaultDepthTest === undefined && (this.defaultDepthTest = !!this._viewer.scene.globe.depthTestAgainstTerrain);

  let ray = this._viewer.camera.getPickRay(mousePosition);

  if (objectsToExclude && !Array.isArray(objectsToExclude)) {
    objectsToExclude = [objectsToExclude];
  }

  let pickObj = this._viewer.scene.pickPosition(mousePosition);
  if (!pickObj) {
    //开启深度检测
    this._viewer.scene.globe.depthTestAgainstTerrain = true;
    pickObj = this._viewer.scene.pickFromRay(ray, objectsToExclude);
  } else {
    pickObj = {
      position: pickObj
    };
  }
  // let pickObj = this._viewer.scene.pickFromRay(ray, objectsToExclude);
  let cartesian;
  if (!pickObj || !pickObj.position || !this._viewer.scene.globe.depthTestAgainstTerrain) {
    cartesian = this._viewer.scene.globe.pick(ray, this._viewer.scene);
  } else {
    cartesian = pickObj.position;
  }


  // 还原深度检测
  this._viewer.scene.globe.depthTestAgainstTerrain = !!this.defaultDepthTest;
  this.defaultDepthTest = undefined;

  if (!cartesian) {
    console.log('未拾取到坐标！');
    return;
  }

  return cartesian;
};

/**
 * 获取鼠标当前经纬度
 * @param {object} [movement] 鼠标屏幕位置
 * @param {Array/Object} [objectsToExclude] 排除的实体对象
 * @returns {Degrees}
 **/
getPosition.prototype.getMouseDegrees = function (movement, objectsToExclude) {
  let cartesian = this.getMousePosition(movement, objectsToExclude);

  if (!cartesian) {
    return;
  }
  return this._core.toDegrees(cartesian);
};


export default getPosition